<?php
	ob_start();
	session_start();

	function __autoload($class_name) {
		include './'.$class_name . '.php';
	}
	
	$productId = null;
	$attributes = null;
	
	if(count($_GET) == 2){
		$attributes = $_GET['attributes'];
	}

	$productId = $_GET['productId'];
	
	$dbConfig = new DBConfigNew(); 
	$conn = $dbConfig->getConnection();
	$mysqli=$conn;
	$productId = stripslashes($productId);
	$productId = mysql_real_escape_string($productId);
	$attributes = stripslashes($attributes);
	$attributes = mysql_real_escape_string($attributes);
	$result = null;
	$orderdetails = null;
	$query = null;

	if(count($_GET) == 2 && !empty($attributes)){
		$query = "CALL hambaaco_merchandize.attribute_product_q(".$productId.",'".$attributes."');";
	} else if(count($_GET) == 2) {
		$query = "CALL hambaaco_merchandize.product_info_q(".$productId.");";
	}
	$conn->multi_query($query);	
	$productInfo = array();
	$attribute = array();
	$property = array();
	$recommendedProd = array();
	$results = array();
	$count = 1;


		/* store first result set :: Retrieves Product Information  */
		if ($result = $conn->store_result()) {
			$row = $result->fetch_array();
			$productInfo['productId'] = $row['id_product'];
			$productInfo['productName'] = $row['tx_nm_product'];
			$productInfo['productDesc'] = $row['tx_desc_product'];
			$productInfo['brandName'] = $row['tx_name_brand'];
			$productAvailablity = 'In Stock';
			$availableQuantity = 5;
			if($row['in_saleable'] == 'N'){
				$productAvailablity = 'Discontinued';
				$availableQuantity = 0;
			} else if($row['in_saleable'] == 'Y' && $row['tx_quantity']<1){
				$productAvailablity = 'Out of Stock';
				$availableQuantity = 0;
			}else if($row['tx_quantity']<5){
				$availableQuantity = $row['tx_quantity'];
			}
			$productInfo['availableQuantity'] = $availableQuantity;
			$productInfo['productAvailability'] = $productAvailablity;
			$productInfo['productPrice'] = $row['tx_price'];
			$productInfo['productDiscountPercent'] = $row['tx_discount'];
			$discountedPrice = ($row['tx_price']/100)*(100 - $row['tx_discount']);
			$productInfo['productDiscountPrice'] = floor($discountedPrice);
			$productInfo['productDiscount'] = $row['tx_price'] - floor($discountedPrice);
			if($row['tx_price']<300){
				$productInfo['shipping'] = '+ Rs 50 (Shipping)';
			} else {
				$productInfo['shipping'] = 'Free Shipping';
			}
			$result->close();
		}
		
		if ($conn->more_results()) {
			$count++;
			$conn->next_result();
		}
		
		/* store second result set :: Retrieves Product Attribute  */		
		if ($count == 2 && $result = $conn->store_result()) {
			while($row = $result->fetch_array()){
				if(!isset($attribute[ucwords($row['tx_nm_attribute'])])){
					$attribute[ucwords($row['tx_nm_attribute'])] = array();					
				}
				array_push($attribute[ucwords($row['tx_nm_attribute'])],$row['tx_value']);
			}
			$result->close();
		}
			
		if ($conn->more_results()) {
			$count++;
			$conn->next_result();
		}
		
		/* store theird result set :: Retrieves Product Property  */			
		if ($count == 3 && $result = $conn->store_result()) {
			while($row = $result->fetch_array()){
				if(!isset($property[$row['tx_prop_category']])){
					$property[$row['tx_prop_category']] = array();					
				}
				$property[$row['tx_prop_category']][$row['tx_prod_prop_key']] = $row['tx_property_value'];
			}
			$result->close();
		}
			
		if ($conn->more_results()) {
			$count++;
			$conn->next_result();
		}
		
		/* store found result set :: Retrieves Product Attribute  */		
		if ($count == 4 && $result = $conn->store_result()) {
			while($row = $result->fetch_array()){
				$tempArr = array();
				$tempArr['productId'] = $row['id_product'];
				$tempArr['productName'] = $row['tx_nm_product'];
				$tempArr['productPrice'] = $row['tx_price'];
				$tempArr['productDiscountPercent'] = $row['tx_discount'];
				$discountedPrice = ($row['tx_price']/100)*(100 - $row['tx_discount']);
				$tempArr['productDiscountPrice'] = floor($discountedPrice);
				$tempArr['productDiscount'] = $row['tx_price'] - floor($discountedPrice);
				array_push($recommendedProd,$tempArr);
			}
			$result->close();
		}
			

		if($count == 4){
			$attributeKeys = array_keys($attribute); 
			$attrArray = array();
			foreach ($attributeKeys as $key){
				$tmpArr = array();
				$tmpArr['key'] = $key;
				$tmpArr['value'] = $attribute[$key];
				array_push($attrArray,$tmpArr);
			}
		
			$propertyArr = array();
			$propertyCategoryList = array_keys($property);
			foreach ($propertyCategoryList as $propertyCategory){
				$propArr = array();
				$tmpArr2 = array();	
				$propertyListKey = array_keys($property[$propertyCategory]);
				$propertyList = $property[$propertyCategory];
				foreach($propertyListKey as $prop){
					$tmpArr1 = array();
					$tmpArr1['key'] = $prop;
					$tmpArr1['value'] = $propertyList[$prop];
					array_push($tmpArr2,$tmpArr1);
				}
				$propArr['key'] = $propertyCategory;
				$propArr['value'] = $tmpArr2;
				array_push($propertyArr,$propArr);
			}
				
			$results['#success'] = "true";
			$results['product'] = $productInfo;
			$results['attributes'] = $attrArray;
			$results['properties'] = $propertyArr;
			$results['similar'] = $recommendedProd;
				
		} else	{
			$results['#success'] = "false";
		}
				
				
	//echo json_encode($propertyArr);	
		
		//$arr1 = array('t','y','u','i');
		//$arr2 = array('1'=>'a',2=>'b',3=>$arr1,4=>'d');
		//$arr2['1']='yyy';
		//$arr = array('a' => $arr2, 'b' => '2', 'c' => '3', 'd' => '4', 'e' => '5');
	
	$dbConfig->closeConnection();
	echo json_encode($results);
	ob_end_flush();
?>